package com.djzhou.gmms.login.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.systeminfo.TbSystemInfo;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.util.properties.PropUtil;
import com.djzhou.gmms.util.session.SessionModel;
import com.djzhou.gmms.util.session.UserSession;
import com.djzhou.util.DateUtil;
import com.djzhou.util.MD5;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends BaseAction<TbUser> implements ModelDriven<TbUser> {

	private static final long serialVersionUID = -7899339288721792882L;
	private TbUser user;
	private String username;
	private String password;
	private TbSystemInfo systemInfo;
	
	/**
	 * 登录方法
	 * @return
	 *
	 */
	public String login(){
		password=MD5.toMD5(password);
		user=userService.queryUser4Login(username, password);
		if(user==null)successFlag=0;
		else if(user.getUserState()==0)successFlag=2;
		else{
			successFlag=1;
			systemInfo=systemInfoService.queryById(1);
			if(systemInfo.getOpenFlag()==0 && user.getUserId()!=1){
				successFlag=11;
				return SUCCESS;
			}
			ServletActionContext.getRequest().getSession().setAttribute("sessionUser", user);
			
			List<TbRole> roleList=userRoleService.queryRoleListByUserId(user.getUserId());
			String roleIds=userRoleService.queryRoleIdsByUserId(user.getUserId());
			String roleNames=userRoleService.queryRoleNamesByUserId(user.getUserId());
			TbRole mainRole=userRoleService.queryMainRoleByUserId(queryUser().getUserId());
			Integer areaId=user.getAreaId();
			String areaName=user.getAreaName();
			ServletActionContext.getRequest().getSession().setAttribute("sessionRoleList", roleList);
			ServletActionContext.getRequest().getSession().setAttribute("sessionRoleIds", roleIds);
			ServletActionContext.getRequest().getSession().setAttribute("sessionRoleNames", roleNames);
			ServletActionContext.getRequest().getSession().setAttribute("mainRole", mainRole);
			ServletActionContext.getRequest().getSession().setAttribute("sessionAreaId", areaId);
			ServletActionContext.getRequest().getSession().setAttribute("sessionAreaName", areaName);
			
			System.out.println("检测到在"+DateUtil.getNowTime("yyyy-MM-dd HH:mm:ss")+"有用户登录,登录名:"+user.getLoginName()+",所属公司:"+user.getCompany().getCompanyName()+",主角色:"+mainRole.getRoleName()+",所属部门:"+user.getDepartment().getDepartmentName()+",所属职位:"+user.getPosition().getPositionName());
			
			
			
			ServletActionContext.getRequest().getSession().setAttribute("systemInfo", systemInfo);
			SessionModel sessionModel = new SessionModel();
			sessionModel.setUser(user);
			sessionModel.setRoleIds(roleIds);
			sessionModel.setRoleList(roleList);
			sessionModel.setRoleNames(roleNames);
			sessionModel.setMainRole(mainRole);
			sessionModel.setSessionIsInstance(Boolean.TRUE);
			
			
			
			//检查是否开启部门层级权限
			for(TbRole r:roleList){
				if(r.getDepAuthorityFlag()==1){
					sessionModel.setChildDepartmentIds(getChildDepartmentIds());
					sessionModel.setOpenComFlag(true);
					break;
				}else{
					sessionModel.setChildDepartmentIds("");
				}
			}
			//检查是否开启公司层级权限
			for(TbRole r:roleList){
				if(r.getCompanyAuthorityFlag()==1){
					sessionModel.setChildCompanyIds(getChildCompanyIds());
					sessionModel.setOpenDepFlag(true);
					break;
				}else{
					sessionModel.setChildCompanyIds("");
				}
			}
			
			System.out.println(getChildDepartmentIds());
			System.out.println(getChildCompanyIds());
			
			
			String projectModel=PropUtil.loadStatistic("config", "PROJECT_MODEL");
			
			
			
			Map<String, Object> sessionMap=new HashMap<String, Object>();
			sessionModel.setSessionMap(sessionMap);
			ServletActionContext.getRequest().getSession().setAttribute("sessionModel", sessionModel);
			UserSession.setSessionModel(sessionModel, Boolean.TRUE);
			
			if(projectModel.equalsIgnoreCase("develop") || user.getUserId()==1){
				//开发模式 权限全部放开
				ServletActionContext.getRequest().getSession().setAttribute("DEVELOP_MODEL", true);
			}else {
				//发行模式 权限部分封锁
				ServletActionContext.getRequest().getSession().setAttribute("DEVELOP_MODEL", false);
			}
			
			logService.addLog("用户登录",getUser(),"登录",user.getUserId(),"TbUser",queryCompany(),super.queryDepartment());
			successFlag=systemInfoService.queryById(1).getSystemStyle();
		}
		return SUCCESS;
	}
	
	public TbUser getModel() {
		return user;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public TbSystemInfo getSystemInfo() {
		return systemInfo;
	}

	public void setSystemInfo(TbSystemInfo systemInfo) {
		this.systemInfo = systemInfo;
	}
	

}
